package com.huluobo.a2207douyindemo.setting;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.databinding.ActivitySettingBinding;
import com.huluobo.a2207douyindemo.utils.ImageUtils;
import com.huluobo.base.bean.UserBeanResult;
import com.huluobo.base.db.AppDBUtils;
import com.huluobo.base.mvvm.BaseMVVMActivity;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

public class SettingActivity extends BaseMVVMActivity<ActivitySettingBinding, SettingViewModel> {
    private UserBeanResult.UserBean userBean;

    @Override
    protected void initData() {
        userBean = (UserBeanResult.UserBean) getIntent().getSerializableExtra("userBean");
        ImageUtils.getInstance().loadImage(this, userBean.getIcon(), binding.settingUserIconIv);
        binding.settingAddressEt.setHint(userBean.getAddress());
        binding.settingNicknameEt.setHint(userBean.getNickname());
        binding.settingBrithEt.setHint(userBean.getBirth());
        binding.settingSexEt.setHint(userBean.getSex());
        vm.updateIconSuccess.observe(this, commonBeanResult -> {
            ToastUtils.showLong(commonBeanResult.getMessage());
            ImageUtils.getInstance().loadImage(this, commonBeanResult.getData(), binding.settingUserIconIv);
            userBean.setIcon(commonBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
        });

        vm.updateIconFailed.observe(this, s -> {
            Log.i("TagA", "上传头像失败:" + s);
        });

        vm.updateAddressSuccess.observe(this, commonBeanResult -> {
            userBean.setAddress(commonBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
        });

        vm.updateAddressFailed.observe(this, s -> {
            Log.i("TagA", "更新地址失败:" + s);
        });
        vm.updateNicknameSuccess.observe(this, commonBeanResult -> {
            userBean.setNickname(commonBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
        });

        vm.updateNicknameFailed.observe(this, s -> {
            Log.i("TagA", "更新昵称失败:" + s);
        });
        vm.updateBrithSuccess.observe(this, commonBeanResult -> {
            userBean.setBirth(commonBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
        });

        vm.updateBrithFailed.observe(this, s -> {
            Log.i("TagA", "更新生日失败:" + s);
        });
        vm.updateSexSuccess.observe(this, commonBeanResult -> {
            userBean.setSex(commonBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
        });

        vm.updateSexFailed.observe(this, s -> {
            Log.i("TagA", "更新性别失败:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.settingUserIconIv.setOnClickListener(v -> {
            selectPic();
        });

        binding.settingUserIconTv.setOnClickListener(v -> {
            selectPic();
        });

        binding.settingNicknameBtn.setOnClickListener(v -> {
            String nickname = binding.settingNicknameEt.getText().toString();
            if (nickname.isEmpty()) {
                ToastUtils.showLong("昵称不能为空");
            } else {
                vm.updateNickname(nickname);
            }
        });
        binding.settingAddressBtn.setOnClickListener(v -> {
            String address = binding.settingAddressEt.getText().toString();
            if (address.isEmpty()) {
                ToastUtils.showLong("地址不能为空");
            } else {
                vm.updateAddress(address);
            }
        });
        binding.settingBrithBtn.setOnClickListener(v -> {
            String brith = binding.settingBrithEt.getText().toString();
            if (brith.isEmpty()) {
                ToastUtils.showLong("生日不能为空");
            } else {
                vm.updateBrith(brith);
            }
        });
        binding.settingSexBtn.setOnClickListener(v -> {
            String sex = binding.settingSexEt.getText().toString();
            if (sex.isEmpty()) {
                ToastUtils.showLong("性别不能为空");
            } else {
                vm.updateSex(sex);
            }
        });
    }

    private void selectPic() {
        PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            PictureBean pictureBean = bundle.getParcelable(PictureSelector.PICTURE_RESULT);
            String path = pictureBean.getPath();
            vm.updateUserIcon(path);
        }
    }

    @Override
    protected Class<SettingViewModel> getViewModel() {
        return SettingViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }
}